<?php
	require_once('init.php');
	
	if (isset($_GET['pid']) || isset($_POST['pid'])) 
	{	
		//Get Current Page
		if (! isset($_GET['page']) || $_GET['page'] == '') {$page = '1';} 
		else {$page = $_GET['page'];}
		
		//Set Shown Form Default New
		$isShow = 'N';
		
		if (isset($_GET['pid'])) 
		{
			$pid = $_GET['pid'];
		}
		else 
		{
			$pid = $_POST['pid'];
		}
		$smarty->assign('pid',$pid);
		
		if (isset($_GET['c']) && $_GET['c'] != '') {
			$c = $_GET['c'];
			if ($c == 'version') 
			{
				$relate = $adminDB->executeSQL("select * from ".$util->contentdbName().".tbRelate where fdProductID = '".$_GET['rpid']."' and fdRelateID = '".$_GET['rrid']."' and fdTypeID = 1", $connID);
				if(!$relate)
				{
					//echo "<script>alert('insert');</script>";
					$relate = $adminDB->executeSQL("insert into ".$util->contentdbName().".tbRelate (fdProductID,fdRelateID,fdTypeID) values ('".$_GET['rpid']."','".$_GET['rrid']."',1)", $connID);
				}
				else
				{
					//echo "<script>alert('delete');</script>";
					$relate = $adminDB->executeSQL("delete from ".$util->contentdbName().".tbRelate where fdProductID = '".$_GET['rpid']."' and fdRelateID = '".$_GET['rrid']."' and fdTypeID = 1", $connID);
				}
			}
			if ($c == 'license') 
			{
				if(!$relate = $adminDB->executeSQL("select * from ".$util->contentdbName().".tbRelate where fdProductID = '".$_GET['rpid']."' and fdRelateID = '".$_GET['rrid']."' and fdTypeID = 2", $connID))
				{
					$relate = $adminDB->executeSQL("insert into ".$util->contentdbName().".tbRelate (fdProductID,fdRelateID,fdTypeID) values ('".$_GET['rpid']."','".$_GET['rrid']."',2)", $connID);
				}
				else
				{
					$relate = $adminDB->executeSQL("delete from ".$util->contentdbName().".tbRelate where fdProductID = '".$_GET['rpid']."' and fdRelateID = '".$_GET['rrid']."' and fdTypeID = 2", $connID);
				}
			}
		}
		
		$product = $adminDB->executeSQL("select p.id,p.fdName,p.fdTarget,p.fdListPrice,p.fdPrice,p.fdAvailabe,p.fdExpire,p.fdMemo,p.fdDescription,p.fdLicense,p.fdVersion,m.id as fdColumnID, m.fdName as fdColumnName from ".$util->contentdbName().".tbProduct p left outer join ".$util->contentdbName().".tbContent t on t.id = p.fdContentID left outer join ".$util->contentdbName().".tbIndex i on i.fdContentID = p.fdContentID left outer join ".$util->contentdbName().".tbColumn m on m.id = i.fdColumnID where p.id = '".$pid."'", $connID);
		$smarty->assign('product', $product);
				
		$sql = "select p.id,p.fdName,p.fdVersion,p.fdLicense,(select count(*) from ".$util->contentdbName().".tbRelate where fdTypeID = 1 and fdProductID ='".$pid."' and fdRelateID = p.id) as RPV, (select count(*) from ".$util->contentdbName().".tbRelate where fdTypeID = 2 and fdProductID ='".$pid."' and fdRelateID = p.id) as RPL, (select count(*) from ".$util->contentdbName().".tbRelate where fdTypeID = 1 and fdRelateID ='".$pid."' and fdProductID = p.id) as RRV, (select count(*) from ".$util->contentdbName().".tbRelate where fdTypeID = 2 and fdRelateID ='".$pid."' and fdProductID = p.id) as RRL from ".$util->contentdbName().".tbProduct p where p.id != '".$pid."' and p.fdContentID in (select i.fdContentID from ".$util->contentdbName().".tbIndex i where i.fdColumnID = '".$product[0]['fdColumnID']."') and p.fdStatus != 3";			
		$relates = $pageDB->pageData($sql, $connID, 20, $page);			
		$smarty->assign('relates', $relates);
		
		$smarty->assign('isShow', $isShow);
		$smarty->display('admin_listrelated.html');
	}
	
	$connDB->closeConnID();

?>